
//1. 在电脑D盘下创建一个文件为HelloWorld.txt文件，判断他是文件还是目录，再创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去


package HomeWork2;


import java.io.File;
import java.io.IOException;


public class FileCreateHomeWork1 {

    public void createFile( ){

        File file=new File("D:\\HelloWorld.txt");
        try {
            file.createNewFile();
            System.out.println(file.getName()+"创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean wj=file.isFile();
        boolean ml=file.isDirectory();
        if (wj==true){
            System.out.println(file.getName()+"是文件");
        }else if(ml==true){
            System.out.println(file.getName()+"是目录");
        }
        file=new File("D:\\IOTest");
        file.mkdir();

        String fromPath = "D:\\HelloWorld.txt";
        String toPath = "D:\\IOTest";
        System.out.println("从 " + fromPath + " 移动到 " + toPath);
        file = new File("D:\\HelloWorld.txt");
        if (file.isFile()) {
            File toFile = new File("D:\\IOTest" + "\\"+ file.getName());
            if (toFile.exists()) {
                System.out.println("文件已存在");
            } else {
                file.renameTo(toFile);
                System.out.println("移动文件成功");
            }
        }

    }



    public static void main(String[] args) {
        FileCreateHomeWork1 fhw=new FileCreateHomeWork1();
        fhw.createFile();
    }
}
